package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * @author: mosin
 * @version: v1.0  2022/9/6
 */
@WebServlet("/list")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // TODO 查询数据库获取用户信息列表
        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i <5 ; i++) {
            User user = new User();
            user.setId(i);
            user.setPassword("1234"+i);
            user.setUsername("admin"+i);
            user.setAge(20+i);
            String sex  = i%2==0?"男":"女";
            user.setSex(sex);
            users.add(user);
        }

        // 将查询最终的结果传递到jsp页面进行数据的展示
        req.setAttribute("users", users);
        req.getRequestDispatcher("/userlist.jsp").forward(req, resp);

    }
}
